package risk.control.application.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.controls.Audio;
import com.otaliastudios.cameraview.controls.Facing;
import com.otaliastudios.cameraview.controls.Flash;
import com.otaliastudios.cameraview.controls.Grid;
import com.otaliastudios.cameraview.controls.Hdr;
import com.otaliastudios.cameraview.controls.WhiteBalance;
import com.otaliastudios.cameraview.size.SizeSelectors;

import java.util.HashMap;

import risk.control.application.R;
import risk.control.application.activity.base.BaseActivity;
import risk.control.application.utils.data.TempCacheUtils;


public class IDCarScanActivity extends BaseActivity implements LifecycleOwner {

    public static int TAKE_PIC_OK_CODE = 7009;

    private CameraView cameraView;
    private Button btnTakePhoto;
    private ImageView IvFlash;
    private ImageView IvBack;

    //是否开启闪光灯
    private boolean mFlashIsOpen = false;

    @Override
    protected void postData(HashMap<String, String> dataMap) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁止截屏录屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        //设置状态栏颜色
        getWindow().setStatusBarColor(Color.parseColor("#000000"));
        setContentView(R.layout.activity_id_car);
        intiView();
        intiData();
        initListener();
    }


    private void intiView() {
        cameraView = findViewById(R.id.camera);
        btnTakePhoto = findViewById(R.id.btn_take_photo);
        IvFlash = findViewById(R.id.iv_flash);
        IvBack = findViewById(R.id.iv_back);
    }

    private void intiData() {
        //相机设置
        cameraView.setAudio(Audio.OFF);//不需要录音
        //传感器控制
        cameraView.setFacing(Facing.BACK);// 后置摄像头
        cameraView.setHdr(Hdr.OFF);// 关闭HDR
        cameraView.setWhiteBalance(WhiteBalance.DAYLIGHT);//白天模式
        cameraView.setGrid(Grid.OFF);  // 添加网格线
        cameraView.setFacing(Facing.BACK);          // 后置摄像头
        cameraView.setFlash(Flash.OFF);//关闭闪光灯
        cameraView.setPictureSize(SizeSelectors.biggest()); // 使用最大可用尺寸
//        cameraView.setPictureSize(SizeSelectors.maxWidth(800));//限制宽度（保持宽高比）降低图片质量
        cameraView.setZoom(0);          // 缩放级别(1.0=无缩放)
        cameraView.setPlaySounds(true);     // 是否播放系统相机声音
        // 设置相机生命周期
        cameraView.setLifecycleOwner(this);
    }

    private void initListener() {
        cameraView.addCameraListener(new CameraListener() {
            @Override
            public void onPictureTaken(@NonNull PictureResult result) {
                super.onPictureTaken(result);
                handleResult(result);
            }
        });

        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraView.takePicture();
            }
        });

        IvFlash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mFlashIsOpen) {
                    //关闭状态
                    mFlashIsOpen = true;
                    cameraView.setFlash(Flash.ON);
                    IvFlash.setImageResource(R.mipmap.bd_ocr_light_on);
                } else {
                    //开启状态
                    mFlashIsOpen = false;
                    cameraView.setFlash(Flash.OFF);
                    IvFlash.setImageResource(R.mipmap.bd_ocr_light_off);
                }
            }
        });

        IvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //处理图片
    private void handleResult(PictureResult result) {
        byte[] photoData = result.getData();
        TempCacheUtils.getInstance().put(TempCacheUtils.ID_CAR_PIC_CACHE_KEY, photoData);
        Intent returnIntent = new Intent();
        returnIntent.putExtra("key", "value");
        setResult(TAKE_PIC_OK_CODE, returnIntent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (cameraView != null) {
            cameraView.open();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (cameraView != null) {
            cameraView.close();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (cameraView != null) {
            cameraView.destroy();
        }
    }

}





